package org.ucas.faker.web.utils.tech;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PatentFeaturize1 {
	
	private List<String> features = null;
	private List<String> effects = null;
	private PatentFeaturize1(){
		features = new ArrayList<>();
		effects = new ArrayList<>();
		try {
            File file=new File("feature.rule");
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                new FileInputStream(file));//考虑到编码格式
                BufferedReader featureReader =  new BufferedReader(read);
                String lineTxt = null;
                while((lineTxt = featureReader.readLine()) != null){
                    //System.out.println(lineTxt);
                    features.add(lineTxt);
                }
                read.close();
	    }else{
	        System.out.println("Error finding feature.rule");
	    }
	    } catch (Exception e) {
	        System.out.println("Error reading feature.rule");
	        e.printStackTrace();
	    }
		
		
		try {
			File file=new File("effect.rule");
	        if(file.isFile() && file.exists()){ //判断文件是否存在
	        	InputStreamReader read = new InputStreamReader(
	            new FileInputStream(file));//考虑到编码格式
	        	BufferedReader featureReader =  new BufferedReader(read);
	            String lineTxt = null;
	            while((lineTxt = featureReader.readLine()) != null){
	                //System.out.println(lineTxt);
	                effects.add(lineTxt);
	            }
	            read.close();
	    }else{
	        System.out.println("Error finding effect.rule");
	    }
	    } catch (Exception e) {
	        System.out.println("Error reading effect.rule");
	        e.printStackTrace();
	    }		
	}
	
	private static PatentFeaturize1 singleton = null;
	
	public static PatentFeaturize1 getInstance(){
		if(singleton==null){
			singleton = new PatentFeaturize1();
		}
		return singleton;
		
	}

	public Map<String,List<String>> featurize(String text, List<String> keywords){
		Map<String,List<String>> resultMap = new HashMap<>();
		List<String> lfeatures = new ArrayList<>();
		List<String> leffects = new ArrayList<>();
		List<String> lunknown = new ArrayList<>();
		
		String [] res = text.split("，|；|。");//:,
		
		/*
		 for(String s : res){
			System.out.println(s);
		}
		System.out.println("\n");
		*/
		List<String> sentences = new ArrayList<>();
		for(String key : keywords){
			sentences.clear();
			//System.out.println("un\t:"+key);
			for(String s:res){
				if(s.indexOf(key)!=-1) {sentences.add(s);}
			}
			if(sentences.size()==0){
				//TODO				
				continue;
			}
			//判断属于特征还是功效
			boolean isfeature = false;
			boolean iseffect = false;
			
			for(String sent:sentences){
				
				for (String fea : features){
					if(sent.indexOf(fea)!=-1){
						//System.out.println("feature\t:"+key);
						lfeatures.add(key);
						isfeature = true;
						break;
					}
				}
				if(isfeature) continue;
				
				for(String eff : effects){
					//System.out.println(sent+"\t"+eff);
					if(sent.indexOf(eff)!=-1){
						//System.out.println("effects\t:"+key);
						leffects.add(key);
						iseffect = true;
						break;
					}
				}
				if(iseffect) continue;
			}

			//not classified
			if(!isfeature && ! iseffect){
				lunknown.add(key);
				//System.out.println("unkown\t:"+key);
			}
			
		}
		
		resultMap.put("feature", lfeatures);
		resultMap.put("effect", leffects);
		resultMap.put("unknown", lunknown);
		
		return resultMap;
		/*
		for (String s : res){
			for (String key : features){
				if(s.indexOf(key)!=-1){
					System.out.println("feature\t:"+s);
					break;
				}
			}
			for(String key : effects){
				if(s.indexOf(key)!=-1){
					System.out.println("effects\t:"+s);
					break;
				}
			}
		}*/
	}
	
	public void displayResult(Map<String,List<String>> map){
		List<String> l = map.get("feature");
		System.out.print("Feature: ");
		for(String s : l){
			System.out.print(s+"\t");
		}
		
		l = map.get("effect");
		System.out.print("\nEffect: ");
		for(String s : l){
			System.out.print(s+"\t");
		}
		
		l = map.get("unknown");
		System.out.print("\nUnknown: ");
		for(String s : l){
			System.out.print(s+"\t");
		}
		
	}
	
	public void TestFromFile(String filepos){
		int order = 1;
		try {
        File file=new File(filepos);
        if(file.isFile() && file.exists()){ //判断文件是否存在
            InputStreamReader read = new InputStreamReader(
            new FileInputStream(file));//考虑到编码格式
            BufferedReader featureReader =  new BufferedReader(read);
            String lineTxt = null;
            while((lineTxt = featureReader.readLine()) != null){
                //System.out.println(lineTxt);
            	System.out.println("order "+order);
            	order++;
                String [] oneline = lineTxt.split("  ");
                List<String> lkeyword = new ArrayList<>();
                for(String s : oneline[1].split(",")){
                	lkeyword.add(s);
                }
                displayResult(featurize(oneline[0],lkeyword));
                
                
                lineTxt = featureReader.readLine();
                System.out.println("\n");
            }
            read.close();
    }else{
        System.out.println("Error finding feature.rule");
    }
    } catch (Exception e) {
        System.out.println("Error reading feature.rule");
        e.printStackTrace();
    }
		
	}
	
	public void TestFromFile2(String filepos){
		int order = 1;
		try {
        File file=new File(filepos);
        if(file.isFile() && file.exists()){ //判断文件是否存在
            InputStreamReader read = new InputStreamReader(
            new FileInputStream(file));//考虑到编码格式
            BufferedReader featureReader =  new BufferedReader(read);
            String lineTxt = null;
            while((lineTxt = featureReader.readLine()) != null){
                //System.out.println(lineTxt);
            	System.out.println("order "+order);
            	order++;
                String [] oneline = lineTxt.split("  ");
                List<String> lkeyword = new ArrayList<>();
                oneline[1] = oneline[1].split("##")[0];
                for(String s : oneline[1].split("\\|")){
                	lkeyword.add(s);
                }
                displayResult(featurize(oneline[0],lkeyword));     
                
                lineTxt = featureReader.readLine();
                System.out.println("\n");
            }
            read.close();
    }else{
        System.out.println("Error finding feature.rule");
    }
    } catch (Exception e) {
        System.out.println("Error reading feature.rule");
        e.printStackTrace();
    }
		
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*
		String abs = "本发明的技术方案包括一种实现游戏人工智能真实感视觉的方法，该方法包括：创建椭圆曲线构造人工智能角色的视觉模型；初始化视觉模型，并智能计算并检测视距范围内的物体、角色；离散方法在视觉模型内部构建新的小椭圆曲线，并划分区域，每个区域都有对应的辨识概率；实时检测自身行为以及物体、角色的行为变化，动态调整辨识概率。发明的有益效果为：消耗游戏程序的资源少；视觉模型给游戏NPC连贯并详细的对游戏世界的认识；同时视觉模型给游戏NPC带来更多的细节以及真实感；增强了游戏的表现力与玩家的代入感，提高游戏的乐趣与游戏性。";
		List<String> keywords = new ArrayList<String>();
		keywords.add("离散方法");
		keywords.add("视觉模型");
		keywords.add("真实感");
		keywords.add("代入感");
		
		keywords.clear();
		abs = "本实用新型公开了一种方型铝壳锂离子电池电芯结构，属于锂离子电池制造领域；旨在提供一种平整度高、极片之间的界面稳定性高、循环性能稳定的卷绕式方型铝壳锂离子电池电芯结构，本实用新型包括有电池正负极片；其特征是：所述电池极片上，根据卷绕半周宽度的间隔，设有涂层空隙。提高了整个电池卷芯的平整性，采用本实用新型电芯结构的电池稳定性高、循环性能稳定性高。适用于锂离子电池的制造领域。";
		
		
		
		for(String s : "旨在,宽度,间隔,涂层空隙,卷绕式,方型,制造领域,锂离子电池,实用新型公开,平整度,界面稳定性,循环性能稳定,有电池正负极,电池极片,卷绕半,平整性,实用新型电芯结构,电池稳定性,循环性能稳定性".split(",")){
			keywords.add(s);
		}*/
		PatentFeaturize1 pf = PatentFeaturize1.getInstance();
		//pf.displayResult(pf.featurize(abs,keywords));
		//System.out.println(abs);
		pf.TestFromFile2("testcase10_1");
	}
	

}
